package jp.ac.u_tokyo.dch.model.output;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.view.Constants;

public class XmlComposite {

	private List<XmlComposite> childList;

	private String tagName;

	private int tagId;

	private int tagOyaId = -1;

	private int index;

	private List<Attribute> attrList;

	private String text;

	private String type;



	public XmlComposite(int tagId, int oyaTagid, int index, String tagName, String type) {

		this.tagId = tagId;
		this.tagOyaId = oyaTagid;
		this.index = index;
		this.tagName = tagName;
		this.type = type;
		attrList = new ArrayList<Attribute>();
		childList = new ArrayList<XmlComposite>();

	}

	public XmlComposite(int tagId, int oyaTagid, int index, String tagName, String type, List<Attribute> attrList, String text) {

		this.tagId = tagId;
		this.tagOyaId = oyaTagid;
		this.index = index;
		this.tagName = tagName;
		this.type = type;
		this.attrList = attrList;
		this.text = text;
		childList = new ArrayList<XmlComposite>();

	}

	public List<XmlComposite> getChildList() {
		return childList;
	}
	public void setChildList(List<XmlComposite> childList) {
		this.childList = childList;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public List<Attribute> getAttrList() {
		return attrList;
	}

	public void setAttrList(List<Attribute> attrList) {
		this.attrList = attrList;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}



	public int getTagId() {
		return tagId;
	}

	public void setTagId(int tagId) {
		this.tagId = tagId;
	}

	public int getTagOyaId() {
		return tagOyaId;
	}

	public void setTagOyaId(int tagOyaId) {
		this.tagOyaId = tagOyaId;
	}

	/**
	 * 子ノードを追加する
	 * @param newTagId
	 * @param oyaId
	 * @param index
	 */
	public void addChild(int newTagId, int oyaId, int index, String tagName, String type) {

		if (tagId == oyaId) {

			childList.add(new XmlComposite(newTagId, oyaId, index, tagName, type));

			return;

		} else {

			for (XmlComposite child : childList) {

				child.addChild(newTagId, oyaId, index, tagName, type);

			}
		}
	}

	/**
	 * 子ノードを追加する
	 * @param newTagId
	 * @param oyaId
	 * @param index
	 */
	public void addChild(int newTagId, int oyaId, int index, String tagName, String type, List<Attribute> attrList, String text) {

		if (tagId == oyaId) {

			childList.add(new XmlComposite(newTagId, oyaId, index, tagName, type, attrList, text));

			return;

		} else {

			for (XmlComposite child : childList) {

				child.addChild(newTagId, oyaId, index, tagName, type, attrList, text);

			}
		}
	}

	public void convXmlDataList(int level, List<XmlData> list) {

		XmlData xmlData = new XmlData();

		xmlData.setTagName(tagName);
		xmlData.setTagId(String.valueOf(tagId));
		xmlData.setIndex(String.valueOf(index));
		xmlData.setLevel(String.valueOf(level));
		xmlData.setOyaTagId(String.valueOf(tagOyaId));
		xmlData.setAttrList(attrList);

		list.add(xmlData);

		for (XmlComposite child : childList) {

			child.convXmlDataList(level + 1, list);
		}


	}

	public String toXml() {

		StringBuilder output = new StringBuilder();

		if (Constants.TAG_TYPE_TEXT.equals(type)) {
			output.append("<");
			output.append(tagName);

			for (Attribute attribute : attrList) {

				output.append(" ");
				output.append(attribute.getName());
				output.append("=");
				output.append("\"");
				output.append(attribute.getValue());
				output.append("\"");
			}
			output.append(">");

			output.append(text);

			for (XmlComposite child : childList) {

				output.append(child.toXml());
			}

			output.append("</");
			output.append(tagName);
			output.append(">");

		} else {
			output.append(Constants.TAG_INSERT_ID);
		}


		return output.toString();

	}


	/**
	 * HTMLを出力する
	 * @param level
	 * @param sort
	 * @param fieldErrors
	 * @return
	 */
	public String toHtml(int level, String sort, Map<String, List<String>> fieldErrors) {

		StringBuilder sb = new StringBuilder();

		if (level == 0) {
			sb.append("<ul id = \"xmltree\" class=\"xmltreecls\"");
		} else {
			sb.append("<ul class=\"xmltreecls\">");
		}
		sb.append("<li class=\"xmltreecls\">");

		//タイプがテキストの場合
		if (Constants.TAG_TYPE_TEXT.equals(type)) {

			if (fieldErrors.containsKey(String.valueOf(tagId))) {
				sb.append("<input type=\"text\" style = \"background-color: #FFE4E1\" name=\"xmlObjList[" + index + "].tagName\" value=\"" + tagName + "\" />");
			} else {
				sb.append("<input type=\"text\" name=\"xmlObjList[" + index + "].tagName\" value=\"" + tagName + "\" />");
			}
			sb.append("<input type= \"button\" value=\"子作成\" style=\"margin: 1px\" onclick=\"createChild('" + tagId +"', '0')\"/>");
			//テキストの場合
			if (Constants.TAG_TYPE_TEXT.equals(type) &&
					(Constants.LV3_SORT.equals(sort) ||
					Constants.LV4_SORT.equals(sort) ||
					Constants.LV5_SORT.equals(sort))) {

				sb.append("<input type= \"button\" value=\"挿入\" style=\"margin: 1px\" onclick=\"createChild('" + tagId +"', '1')\"/>");
			}
		} else {
			sb.append("下段階タグ");
			sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].tagName\" value=\"" + tagName + "\" />");
		}

		sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].tagId\" value=\"" + tagId + "\" />");
		sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].level\" value=\"" + level + "\"/>");
		sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].oyaTagId\" value=\"" + tagOyaId + "\"/>");
		sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].index\" value=\"" + index + "\"/>");
		sb.append("<input type=\"hidden\" name=\"xmlObjList[" + index + "].type\" value=\"" + type + "\"/>");

		if (level != 0) {
			sb.append("<input type= \"button\" value=\"削除\" style=\"margin: 1px\" onclick=\"deleteChild('" + tagId +"')\"/>");
			sb.append("<input type= \"button\" style=\"width: 18px;\" value=\"↓\" style=\"margin: 1px\" onclick=\"orderChange('" + tagId +"')\"/>");
		}

		if (Constants.TAG_TYPE_TEXT.equals(type)) {

			sb.append("<input type= \"button\" id = \"attrBtn" + tagId + "\" class = \"attrBtn\" value=\"属性\"  onclick=\"displayProperty('" + tagId +"')\" style=\"margin: 1px\"/>");

		}

		sb.append("<ul class=\"xmltreecls\">");

		for (XmlComposite child : childList) {

			sb.append(child.toHtml(level + 1, sort, fieldErrors));
		}

		sb.append("</ul>\n");
		sb.append("</li>\n");
		sb.append("</ul>\n");


		return sb.toString();
	}

}
